package com.custom.common.config;


import com.custom.common.util.DateUtils;
import com.custom.model.RequestInfoBean;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 统一日志输出
 *
 * @author Requiem
 * @version 1.0
 * @date 2018/1/25
 */
@Aspect
@Order(5)
@Component
public class WebLogAspect {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    ThreadLocal<Long> startTime = new ThreadLocal<>();


    private String req_name;
    private String args;
    private Long time;
    private Object data;
    @Autowired
    private RedisTemplate redisTemplate;

    public WebLogAspect() {
    }

    @Pointcut("execution(public * com.custom.controller..*.*(..))")
    public void webLog() {
    }

    @Before("webLog()")
    public void doBefore(JoinPoint joinPoint) {
        this.startTime.set(System.currentTimeMillis());
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        req_name = joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName();
        args = Arrays.toString(joinPoint.getArgs());
        System.out.printf("this is qianzhitongzhi");
        this.logger.info("URL : " + request.getRequestURL().toString());
        this.logger.info("HTTP_METHOD : " + request.getMethod());
        this.logger.info("IP : " + request.getRemoteAddr());
        this.logger.info("CLASS_METHOD : " + req_name);
        this.logger.info("ARGS : " + args);
    }

    @AfterReturning(
            returning = "ret",
            pointcut = "webLog()"
    )
    public void doAfterReturning(Object ret) throws Throwable {
        System.out.println("this is houzhitongzhi");
        /**
         * 日志入库
         */
       // insertDB();
        time = (System.currentTimeMillis() - (Long) this.startTime.get());
        data = ret;
        this.logger.info("RESPONSE : " + ret);
        this.logger.info("SPEND TIME : " + time);
    }

    private void insertDB() {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();

        RequestInfoBean info = new RequestInfoBean()
                .setURL(request.getRequestURL().toString())
                .setRe_method(request.getMethod())
                .setIp(request.getRemoteAddr())
                .setClass_method(req_name)
                .setArgs(args)
                .setRes_data(data)
                .setTime(time);

        redisTemplate.opsForValue().set(DateUtils.now(), info);
    }
}

